#How do I display a picture on the screen?

import random
import pygame
from pygame.locals import *

import qgl
import qgle
    
def random_position():
    return random.randint(0,800)-400, random.randint(0,600)-300, 0

def main():
    #setup pygame as normal, making sure to include the OPENGL flag in the init function arguments.
    pygame.init()
    flags =  OPENGL|DOUBLEBUF|HWSURFACE
    pygame.display.set_mode((800,600), flags)
    
    
    #Create two visitors.
    #The compiler visitor is used to change a Node object into a set of OpenGL draw commands. More on nodes later.
    compiler = qgl.render.Compiler()
    #The render visitor is used to execute compiled commands.
    render = qgl.render.Render()
    
    #the root node is the root of the tree structure (also called a scene graph). Branches get added to the root. 
    root_node = qgl.scene.Root()
    
    #every root node must have a viewport branch, which specified which area of the screen to draw to.
    #the OrthoViewport renders all its children in a flat, orthographic view.
    viewport = qgl.scene.OrthoViewport()
    viewport.screen_dimensions = (0,0,800,600)
    
    #a group node can translate, rotate and scale its children. it can also contain leaves, which are drawable things.
    
    
    #leaves are added to the group. The texture leaf loads a texture image ready for drawing. Any quads leaves, which are drawn 
    #after a texture leaf will be rendered with the texture image.
    alien = pygame.image.load("fashion_boy/data/alien.gif")
    xsize , ysize = alien.get_width(), alien.get_height()
    texture = qgl.scene.Texture("fashion_boy/data/alien.gif")
    all = []
            
    #Now we add the different nodes and leaves into a tree structure using the .add method.
    root_node.add(viewport)
    
    for i in range(100):
        quad = qgl.scene.Quad((xsize,ysize))
    
        group = qgl.scene.Group()
        viewport.add(group)
        group.add(texture, quad)
        group.translate = random_position()
        all.append(group)
    
    #Before the structure can be drawn, it needs to be compiled. To do this, we ask the root node to accept the compiler visitor.
    #If any new nodes are added later in the program, they must also accept the compiler visitor before they can be drawn.
    root_node.accept(compiler)
    
    #the main render loop
    clock = pygame.time.Clock()
    while True:
        clock.tick(200)
        for g in all:
            g.translate = random_position()
        #process pygame events.
        for event in pygame.event.get():
            if event.type is QUIT:
                return
            elif event.type is KEYDOWN:
                print "FPS:", clock.get_fps()
                return
        
        #ask the root node to accept the render visitor. This will draw the structure onto the screen.
        #notice that QGL draws everything from the centre, and that the 0,0 point in a QGL screen is the center of the screen.
        root_node.accept(render)
        
        #flip the display
        pygame.display.flip()


main()

